/**
 * 
 */
package com.yh.base.util;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author <a href="mailto:wayne.li.1984@gmail.com">Wayne.Li</a>
 * 
 */
public class SpringBeanUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext paramApplicationContext)
            throws BeansException {
        applicationContext = paramApplicationContext;
    }

    public static Object getBean(String beanName) {
        return applicationContext.getBean(beanName);
    }

    public static <T> T getBean(Class<T> beanType) {
        return (T) BeanFactoryUtils.beanOfType(applicationContext, beanType);
    }

    public static <T> T getBean(Class<T> beanType, boolean includeNonSingletons,
                                boolean allowEagerInit) {
        return (T) BeanFactoryUtils.beanOfType(applicationContext, beanType, includeNonSingletons,
                allowEagerInit);
    }

    public static String getMessage(String propertyKey) {
        return getMessage(propertyKey, null);
    }

    public static String getMessage(String propertyKey, Object[] args) {
        return applicationContext.getMessage(propertyKey, args, null);
    }
}
